<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
	resetForm();
	
	$('#findCollege').keyup(function() {
		searchCollege();
	});
	
	$(document).on('click', '.college', function() {
		resetForm();
		$('#command').val('updateCollege');
		$('#college_no').val($(this).attr('name'));
		$('#college_name').val($(this).find('.college_name').text());
		$('#submitBtn').html('수정');
		$('#resetBtn').show();
		$('#delBtn').show();
		$('#findCollege').val('');
		$('#collegelist *').remove();
	});
	
	$('#submitBtn').click(function() {
		$('#editForm').submit();
	});
	
	$('#delBtn').click(function() {
		location.href="major.do?command=deleteCollege&&college_no="+$('#college_no').val();
	});
	
	$('#resetBtn').click(function() {
		resetForm();
	});
});

function searchCollege() {
	collegehtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getCollegeList&&search="+$('#findCollege').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.collegelist;
			for (var i=0;i<list.length;i++) {
				collegehtml+="<div class='college' name='"+list[i].college_no+"'><div class='college_name'>"+list[i].college_name+"</div></div>";
			}
			$('#collegelist').html(collegehtml);
		}
	});
}

function resetForm() {
	$('#command').val('registerCollege');
	$('#college_no').val('0');
	$('#college_name').val('');
	$('#submitBtn').html('등록');
	$('#resetBtn').hide();
	$('#delBtn').hide();
}
</script>
</head>
<body>
<a href="index.do?command=menu">메뉴로</a>
<div id="findDiv">
	<input id="findCollege">
	<div id="collegelist"></div>
</div>
<div id="editDiv">
	<div id="formDiv">
		<form action="major.do" method="post" id="editForm">
			<input type="hidden" id="command" name="command" value="registerCollege">
			<input type="hidden" id="college_no" name="college_no" value="0">
			단과대학 이름 <input id="college_name" name="college_name">
		</form>
		<div id="submitBtn">등록</div>
		<div id="delBtn">삭제</div>
		<div id="resetBtn">새 단과대학 등록</div>
	</div>
</div>
</body>
</html>